package com.aplink.generic.dialog;

import java.io.Serializable;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.Html;
import android.text.TextUtils;
import android.widget.EditText;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.constant.type.ActionType;
import com.aplink.generic.constant.type.DialogButtonType;
import com.aplink.generic.constant.type.DialogStatus;
import com.aplink.generic.dictionary.GenericDictionary;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.util.Language;

public class ApLinkAlertDialog implements DialogInterface.OnClickListener,
        Serializable {
	private static final long serialVersionUID = -1655995347950817026L;
	private ActionType mActionType;
	private boolean mCancelable;
	private boolean mCanceledOnTouchOutside;
	private transient DialogInfo mDialogInfo;
	private final GenericDictionary<String, DialogInfo> mDialogInfos;
	private transient DialogStatus mDialogStatus;
	private transient GenericFragment mGenericFragment;
	private EditText mInputEdittext;

	public interface DialogConfirmCallback {
		void onConfirmed();

		void onCanceled();
	}

	private DialogConfirmCallback mDialogConfirmCallback;

	public ApLinkAlertDialog() {
		this.mDialogInfos = new GenericDictionary<String, DialogInfo>();
	}

	public boolean contain(final String key) {
		return this.mDialogInfos.containsKey(key);
	}

	public ActionType getActionType() {
		return mActionType;
	}

	public AlertDialog getAlertDialog(final DialogStatus key) {
		final AlertDialog.Builder builder = new AlertDialog.Builder(
		        this.mGenericFragment.getActivity());
		this.mDialogInfo = this.mDialogInfos.get(key.getValue());
		this.mDialogStatus = key;
		if (mDialogInfo.getText() != null) {
			String message = null;
			if (mDialogInfo.isFormatHTML()) {
				message = Html.fromHtml(mDialogInfo.getText()).toString();
			} else {
				message = mDialogInfo.getText();
			}
			builder.setMessage(Language.getInstance().get(message));
		}
		if (mDialogInfo.getTitle() != null) {
			String title = null;
			if (mDialogInfo.isFormatHTML()) {
				title = Html.fromHtml(mDialogInfo.getTitle()).toString();
			} else {
				title = mDialogInfo.getTitle();
			}
			builder.setTitle(Language.getInstance().get(title));
		}
		if (mDialogInfo.getNegativeButton() != null) {
			String negativeButton = null;
			if (mDialogInfo.isFormatHTML()) {
				negativeButton = Html.fromHtml(
				        mDialogInfo.getNegativeButton().getText()).toString();
			} else {
				negativeButton = mDialogInfo.getNegativeButton().getText();
			}
			builder.setNegativeButton(Language.getInstance()
			        .get(negativeButton), this);
		}
		if (mDialogInfo.getPositiveButton() != null) {
			String positiveButton = null;
			if (mDialogInfo.isFormatHTML()) {
				positiveButton = Html.fromHtml(
				        mDialogInfo.getPositiveButton().getText()).toString();
			} else {
				positiveButton = mDialogInfo.getPositiveButton().getText();
			}
			builder.setPositiveButton(Language.getInstance()
			        .get(positiveButton), this);
		}
		if (mDialogInfo.getNeutralButton() != null) {
			String neutralButton = null;
			if (mDialogInfo.isFormatHTML()) {
				neutralButton = Html.fromHtml(
				        mDialogInfo.getNeutralButton().getText()).toString();
			} else {
				neutralButton = mDialogInfo.getNeutralButton().getText();
			}
			builder.setNeutralButton(Language.getInstance().get(neutralButton),
			        this);
		}
		if (!TextUtils.isEmpty(mDialogInfo.getInputText())) {
			mInputEdittext = new EditText(mGenericFragment.getActivity());
			mInputEdittext.setHint(mDialogInfo.getInputText());
			builder.setView(mInputEdittext);
		}
		return builder.create();
	}

	public AlertDialog getAlertDialog(final DialogStatus key,
	        String customMessage) {
		final AlertDialog.Builder builder = new AlertDialog.Builder(
		        this.mGenericFragment.getActivity());
		this.mDialogInfo = this.mDialogInfos.get(key.getValue());
		this.mDialogStatus = key;
		if (mDialogInfo.getText() != null) {
			String message = null;
			if (TextUtils.isEmpty(customMessage)) {
				customMessage = mDialogInfo.getText();
			}
			if (mDialogInfo.isFormatHTML()) {
				message = Html.fromHtml(customMessage).toString();
			} else {
				message = customMessage;
			}
			builder.setMessage(Language.getInstance().get(message));
		}
		if (mDialogInfo.getTitle() != null) {
			String title = null;
			if (mDialogInfo.isFormatHTML()) {
				title = Html.fromHtml(mDialogInfo.getTitle()).toString();
			} else {
				title = mDialogInfo.getTitle();
			}
			builder.setTitle(Language.getInstance().get(title));
		}
		if (mDialogInfo.getNegativeButton() != null) {
			String negativeButton = null;
			if (mDialogInfo.isFormatHTML()) {
				negativeButton = Html.fromHtml(
				        mDialogInfo.getNegativeButton().getText()).toString();
			} else {
				negativeButton = mDialogInfo.getNegativeButton().getText();
			}
			builder.setNegativeButton(Language.getInstance()
			        .get(negativeButton), this);
		}
		if (mDialogInfo.getPositiveButton() != null) {
			String positiveButton = null;
			if (mDialogInfo.isFormatHTML()) {
				positiveButton = Html.fromHtml(
				        mDialogInfo.getPositiveButton().getText()).toString();
			} else {
				positiveButton = mDialogInfo.getPositiveButton().getText();
			}
			builder.setPositiveButton(Language.getInstance()
			        .get(positiveButton), this);
		}
		if (mDialogInfo.getNeutralButton() != null) {
			String neutralButton = null;
			if (mDialogInfo.isFormatHTML()) {
				neutralButton = Html.fromHtml(
				        mDialogInfo.getNeutralButton().getText()).toString();
			} else {
				neutralButton = mDialogInfo.getNeutralButton().getText();
			}
			builder.setNeutralButton(Language.getInstance().get(neutralButton),
			        this);
		}
		return builder.create();
	}

	public GenericFragment getGenericFragment() {
		return this.mGenericFragment;
	}

	public boolean has(final DialogStatus alertDialogStatus) {
		if (alertDialogStatus == null) {
			return false;
		}
		return this.mDialogInfos.containsKey(alertDialogStatus.getValue());
	}

	public void init(final GenericFragment genericFragment) {
		this.mGenericFragment = genericFragment;

	}

	public boolean isCancelable() {
		return this.mCancelable;
	}

	public boolean isCanceledOnTouchOutside() {
		return this.mCanceledOnTouchOutside;
	}

	@Override
	public void onClick(final DialogInterface dialog, final int which) {
		if (this.mDialogInfo != null) {
			DialogButton dialogButton = null;
			if (which == DialogInterface.BUTTON_POSITIVE) {
				if (mDialogInfo.hasInputText()) {
					GenericObject genericObject = mGenericFragment
					        .getMappingObject();
					String confirmValue = genericObject.getValue(mDialogInfo
					        .getCompareColumn());
					if (mInputEdittext.getText().toString()
					        .equalsIgnoreCase(confirmValue)) {
						dialogButton = this.mDialogInfo.getPositiveButton();
					}
				} else {
					dialogButton = this.mDialogInfo.getPositiveButton();
				}
			} else if (which == DialogInterface.BUTTON_NEUTRAL) {
				dialogButton = this.mDialogInfo.getNegativeButton();
			} else {
				dialogButton = this.mDialogInfo.getNegativeButton();
			}
			if (mDialogStatus.equals(DialogStatus.CONFIRM)) {
				if (dialogButton.getButtonType().equals(
				        DialogButtonType.Positive)) {
					if (mDialogConfirmCallback == null) {
						this.mGenericFragment.onSubmit(null);
					} else {
						mDialogConfirmCallback.onConfirmed();
					}
				} else {
					if (mDialogConfirmCallback != null) {
						mDialogConfirmCallback.onCanceled();
					}
				}
			} else {
				if (dialogButton.getActionType() != null) {
					this.mGenericFragment.getEventHandler().sendAction(
					        dialogButton.getActionType());
				}
				if (dialogButton.isLinkToNextPage()) {
					this.mGenericFragment.nextPage();
				}
				if (dialogButton.isFinishActivity()) {
					this.mGenericFragment.finishActivity();
				}
			}
		}
	}

	public void put(final String key, final DialogInfo dialogInfo) {
		this.mDialogInfos.put(key, dialogInfo);
	}

	public void setActionType(ActionType actionType) {
		mActionType = actionType;
	}

	public void setCancelable(final boolean cancelable) {
		this.mCancelable = cancelable;
	}

	public void setCanceledOnTouchOutside(final boolean canceledOnTouchOutside) {
		this.mCanceledOnTouchOutside = canceledOnTouchOutside;
	}

	public void show(final String key) {
	}

	public DialogConfirmCallback getDialogConfirmCallback() {
		return mDialogConfirmCallback;
	}

	public void setDialogConfirmCallback(
	        DialogConfirmCallback dialogConfirmCallback) {
		mDialogConfirmCallback = dialogConfirmCallback;
	}

	public void removeConfirmCallbackListener() {
		mDialogConfirmCallback = null;
	}
}
